package com.formulas.component.dto;

import java.io.Serial;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 以批记录回复,通常用于条件查询
 * @author yiwannuofulasi
 * @date 2023/6/2 10:18
 */
public class MultiResponse<T> extends Response{

    @Serial
    private static final long serialVersionUID = 1L;
    /**数据集合Collection<T>**/
    private Collection<T> data;

    /**
     * 获取数据集合List<T>
     * @return List<T>
     */
    public List<T> getData() {
        if (null == data) {
            return Collections.emptyList();
        }
        if (data instanceof List) {
            return (List<T>) data;
        }
        return new ArrayList<>(data);
    }

    /**
     * 设置数据集合Collection<T>
     * @param data Collection<T>
     */
    public void setData(Collection<T> data) {
        this.data = data;
    }

    /**
     * 判断数据集合是否为空
     * @return boolean
     */
    public boolean isEmpty() {
        return data == null || data.isEmpty();
    }

    /**
     * 判断数据集合是否不为空
     * @return boolean
     */
    public boolean isNotEmpty() {
        return !isEmpty();
    }

    /**
     * 成功构建响应
     * @return MultiResponse
     */
    public static MultiResponse buildSuccess() {
        MultiResponse response = new MultiResponse();
        response.setSuccess(true);
        return response;
    }

    /**
     * 失败构建响应
     * @param errCode 错误代码
     * @param errMessage 错误消息
     * @return MultiResponse
     */
    public static MultiResponse buildFailure(String errCode, String errMessage) {
        MultiResponse response = new MultiResponse();
        response.setSuccess(false);
        response.setErrCode(errCode);
        response.setErrMessage(errMessage);
        return response;
    }

    /**
     * 指定对象构建批记录回复
     * @param data Collection<T>
     * @return MultiResponse<T>
     * @param <T> Object
     */
    public static <T> MultiResponse<T> of(Collection<T> data) {
        MultiResponse<T> response = new MultiResponse<>();
        response.setSuccess(true);
        response.setData(data);
        return response;
    }
}
